package com.hrsystem.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hrsystem.entity.Employee;
import com.hrsystem.entity.User;
import com.hrsystem.lang.Result;
import com.hrsystem.mapper.UserMapper;
import com.hrsystem.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @Transactional
    @PostMapping("/register")
    public Result register(@RequestBody User user){
        User temp=new User();
        System.out.println(user);
        temp=userService.getById(user.getUid());
        if(temp==null){
            temp=user;
            temp.setUpost("游客");
            System.out.println(temp);
            userService.saveOrUpdate(temp);
            return Result.success(null);
        }else{
            return Result.fail("该账号已存在");
        }

    }

    @Transactional
    @PostMapping("/login")
    public Result login(@RequestBody User user){
        System.out.println(user);
        boolean isVaild=false;
        User temp=new User();
        temp=userService.getById(user.getUid());
        if(temp!=null&&user.getUpassword().equals(temp.getUpassword())){
            isVaild=true;
        }
        if(isVaild){
            return Result.success(temp);
        }else{
            return Result.fail("账号、用户名或密码错误");
        }
        /*User user = userService.findByUserId(id);
        if (user != null && user.getPassword().equals(password) && user.getUsername().equals(username)) {
            LastLoginTime=user.getLoginTime();
            userService.updateLastLoginTime(user.getId());
            isValid=true;
        }
        if (isValid) {
            // 登录成功，直接返回JSON响应
            Map<String, Object> response = new HashMap<>();
            response.put("loginSuccess", true);
            response.put("username", username);
            response.put("lastLoginTime", LastLoginTime);
            System.out.println("Login success");
            return ResponseEntity.ok(response);
        } else {
            // 登录失败，返回错误信息
            Map<String, Object> response = new HashMap<>();
            response.put("loginSuccess", false);
            response.put("errorMessage", "账号、用户名或密码错误");
            System.out.println("Login failed");

            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(response);
        }*/
    }

    @Transactional
    @GetMapping("/permissionlist")
    public Result permissionlist(){
        List<User> users=userService.list();
        return Result.success(users);
    }

    @Transactional
    @PostMapping("/changePost")
    public Result changePost(@RequestBody User user) {
        System.out.println(user);
        User temp=userService.getById(user.getUid());
        temp.setUpost(user.getUpost());
        userService.saveOrUpdate(temp);
        return Result.success(null);

    }
//    @GetMapping("/index")
//    public Result index(){
//        User user = userService.getById(1L);
//        return Result.success(user);
//    }
//
//    @Resource
//    private UserMapper userMapper;
//    @GetMapping("/haha")
//    public void testSelectAll() {
//        List<User> userList = userMapper.selectList(null);
//        userList.forEach(System.out::println);
//    }

}
